<template>
  <el-dialog
    :title="'字典管理-' + type"
    width="1200px"
    top="5vh"
    :visible.sync="visible"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    :modal-append-to-body="false"
    :show-close="false"
  >
    <div class="page">
      <div class="page-form">
        <el-form :inline="true" :model="queryForm">
          <el-form-item label="字典值">
            <el-input v-model="queryForm.code" placeholder="请输入" clearable></el-input>
          </el-form-item>
          <el-form-item label="字典标签">
            <el-input v-model="queryForm.value" placeholder="请输入" clearable></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">查询</el-button>
          </el-form-item>
        </el-form>
      </div>
      <div class="page-table" style="height: 600px;">
        <el-button type="primary" @click="onAdd">新增</el-button>
        <d2-crud
          :loading="loading"
          :columns="tableColumns"
          :data="tableData"
          :rowHandle="rowHandle"
          :pagination="pagination"
          :options="{maxHeight: '400'}"
          @update="onUpdate"
          @delete="onDelete"
          @pagination-current-change="pageChange"
        />
      </div>
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="close">关 闭</el-button>
    </div>

    <!--新增-->
    <dictionary-item-add
      v-if="visibleAdd"
      :visible="visibleAdd"
      :typeId="this.typeId"
      @cancel="cancelAdd"
      @submit="submitAdd"
    />
    <!--修改-->
    <dictionary-item-update
      v-if="visibleUpdate"
      :visible="visibleUpdate"
      :info="this.currRow"
      @cancel="cancelUpdate"
      @submit="submitUpdate"
    />
  </el-dialog>
</template>

<script>
  import DictionaryItemAdd from './dictionary-item-add'
  import DictionaryItemUpdate from './dictionary-item-update'
  import { mapActions } from 'vuex'

  const tableColumns = [
    { title: '字典值', key: 'code' },
    { title: '字典标签', key: 'value' },
    { title: '排序', key: 'orderNum' },
    { title: '备注', key: 'remark' }
  ]
  const rowHandle = {
    width: 180,
    custom: [
      { text: '修改', type: 'primary', size: 'small', emit: 'update' },
      { text: '删除', type: 'danger', size: 'small', emit: 'delete' }
    ]
  }

  export default {
    name: 'dictionary-item',
    props: {
      visible: {
        type: Boolean,
        default: false
      },
      typeId: {
        type: Number,
        default: null
      },
      type: {
        type: String,
        default: ''
      }
    },
    data() {
      return {
        loading: false,
        visibleAdd: false,
        visibleUpdate: false,
        queryForm: {
          typeId: '',
          code: '',
          value: '',
          page: 1,
          limit: 10
        },
        pagination: {
          currentPage: 1,
          pageSize: 10,
          total: 0
        },
        tableColumns,
        rowHandle,
        tableData: [],
        currRow: null
      }
    },
    components: {
      DictionaryItemAdd,
      DictionaryItemUpdate
    },
    mounted () {
      this.$nextTick(() => {
        this.queryList()
      })
    },
    methods: {
      ...mapActions('d2admin/pm.dictionary', [
        'dictItemList',
        'dictItemDelete'
      ]),
      // 关闭自身弹窗
      close() {
        this.$emit('onClose', false)
      },
      // 查询列表
      queryList () {
        this.loading = true
        this.queryForm.typeId = this.typeId
        this.dictItemList(this.queryForm).then(
          res => {
            this.loading = false
            this.tableData = res.list
            this.pagination.total = res.totalCount
          }
        )
      },
      // 提交查询按钮
      onSubmit() {
        this.queryHandle(1)
      },
      // 其他用途调用
      queryHandle (page) {
        this.pagination.currentPage = page
        this.queryForm.page = page
        this.queryList()
      },
      // 切换分页
      pageChange (curr) {
        this.queryHandle(curr)
      },
      // 新增
      onAdd () {
        this.visibleAdd = true
      },
      // 修改
      onUpdate ({ index, row }) {
        this.currRow = row
        this.visibleUpdate = true
      },
      // 删除
      onDelete ({ index, row }) {
        this.$confirm('您确定删除吗？', '提示').then(res => {
          this.dictItemDelete({
            id: row.id
          }).then(res => {
            this.queryHandle(1)
          })
        }).catch(err => {})
      },
      // 取消新增操作
      cancelAdd (visible) {
        this.visibleAdd = visible
      },
      // 提交新增操作
      submitAdd (visible) {
        this.visibleAdd = visible
        this.queryHandle(1)
      },
      // 取消修改操作
      cancelUpdate (visible) {
        this.visibleUpdate = visible
      },
      // 提交修改操作
      submitUpdate (visible) {
        this.visibleUpdate = visible
        this.queryHandle(1)
      }
    }
  }
</script>

<style lang="scss" scoped>
  .page {
    .page-form {
      border: 1px solid #eee;
      padding: 20px 20px 0;
    }
    .page-table {
      border: 1px solid #eee;
      padding: 20px;
      margin-top: 20px;
    }
  }
</style>
